﻿<#
    .Author
    Trevor Sullivan

    .Date
    2014-06-17

    .Description
    Adds a PowerShell ISE add-on menu item, which invokes a command to initialize
    a Mercurial repository at the current working directory.
#>
$DisplayName = '_Init';
$KeyboardShorcut = 'Ctrl + Shift + I';

$Action = {
    $ArgumentList = 'init "{0}"' -f $pwd;
    $Process = Start-Process -FilePath hg.exe -ArgumentList $ArgumentList -PassThru -Wait -NoNewWindow;
    if ($Process.ExitCode -ne 0) { Write-Error -Message ('Error occurred while initializing repository: {0}' -f $Process.ExitCode); };
    };

# Clean up old menu items, with the same name
$HgMenu.Submenus.Where({ $PSItem.DisplayName -match $DisplayName }) | % { $HgMenu.Submenus.Remove($PSItem); };

# Add the new menu item
$HgMenu.Submenus.Add($DisplayName, $Action, $KeyboardShorcut);